#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Create by yanghongqiang
# Create on 2021/3/26
import  requests
from _BAIDU import baidu_storage;
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0",
};
def get_baidus(keyword,page_count):
    baidu_images = [];
    base_url_pattern = "https://image.baidu.com/search/acjson?tn=resultjson_com&logid=11204905288092574406" \
                       "&ipn=rj&ct=201326592&is=&fp=result&queryWord=%s&cl=2&lm=-1&ie=utf-8&oe=utf-8" \
                       "&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word=%s&s=&se=&tab=&width=" \
                       "&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&cg=girl&pn=%d&rn=%d" \
                       "&gsm=b4&1616550957251=";
    page_size = 30;
    url_list = list(base_url_pattern % (keyword, keyword, item * page_size, page_size)
                    for item in range(1, page_count + 1));
    for url in url_list:
        r = requests.get(url,headers = headers);
        if r.status_code ==200:
            images = r.json().get("data")
            for image in  images:
                baidu_images.append(image.get("middleURL"));
            else:
                print("Request error, return status code is: %d" % (r.status_code,));
    print(baidu_images);
    return baidu_images;
if __name__ == '__main__':
    keyword = "清华大学";
    images = get_baidus(keyword,3);
    baidu_storage.image_local("/upload",keyword,images);

